package com.lerays.spider.sougou.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.lionsoul.websnail.Spider;
import org.lionsoul.websnail.json.JSONArray;
import org.lionsoul.websnail.json.JSONObject;
import org.lionsoul.websnail.schedule.TimerItem;

import com.lerays.spider.sougou.SpiderHttpServer;
import com.lerays.spider.sougou.SougouSpider;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

/**
 * default http request handler. <br />
 * 
 * @author chenxin<chenxin619315@gmail.com>
 */
public class StatusRequestHandler implements HttpHandler
{
	private SpiderHttpServer httpServer = null;
	
	public SpiderHttpServer getHttpServer() 
	{
		return httpServer;
	}

	@Override
	public void handle(HttpExchange e) throws IOException 
	{
		JSONObject json = new JSONObject();
		
		//websnail spider
		SougouSpider wechatSpider = httpServer.getsougouSpider();
		Spider spider = wechatSpider.getSpider();
		
		//running status
		int status	= httpServer.getsougouSpider().getStatus();
		String sts	= null;
		if ( status == Spider.SPIDER_RUNNING )
		{
			sts = "running";
		}
		else
		{
			sts = "pause";
		}
		
		json.put("version", wechatSpider.getVersion());
		json.put("status", sts);
		json.put("src_size", wechatSpider.getSourceSize());
		json.put("todo_size", spider.getTodoHandler().size());
		json.put("failed_size", wechatSpider.getFailedTaskCount());
		json.put("active_thd", spider.getWorker().getWorkingThread());
		json.put("last_err_msg", wechatSpider.getLastErrorMsg());
		
		//task start & end time
		json.put("last_start_time", wechatSpider.getLastStartTime());
		json.put("last_end_time", wechatSpider.getRecentEndTime());		
		List<TimerItem> timerItemList = wechatSpider.getSchedule().getItemList();
		JSONArray timerArary = new JSONArray();
		synchronized ( timerItemList )
		{
			JSONObject timerObj = null;
			Iterator<TimerItem> it = timerItemList.iterator();
			while ( it.hasNext() ) 
			{
				TimerItem timerItem = it.next();
				timerObj = new JSONObject();
				timerObj.put("s_time", (int)(timerItem.getStartTime()/1000));
				timerObj.put("delay", (int)(timerItem.getDelay()/1000));
				timerObj.put("period", (int)(timerItem.getPeriod()/1000));
				timerObj.put("n_time", (int)(timerItem.getNextExecTime()/1000));
				timerObj.put("counter", timerItem.getCounter());
				timerArary.put(timerObj);
			}
		}
		
		json.put("schedule", timerArary);
		json.put("last_db_ctime", wechatSpider.getLastDbConnectTime());
		
		//session
		json.put("where", wechatSpider.getWhere());
		json.put("order", wechatSpider.getOrder());
		json.put("limit", wechatSpider.getLimit());
		json.put("interval", wechatSpider.getInterval());
		json.put("callback_hook", wechatSpider.getTaskAccomplishHook());
		//response
		String ret = json.toString();
		e.sendResponseHeaders(200, ret.length());
		OutputStream os = e.getResponseBody();
		os.write(ret.getBytes());
		os.close();
		e.close();
	}
	
	public void setHttpServer(SpiderHttpServer httpServer) 
	{
		this.httpServer = httpServer;
	}

	public StatusRequestHandler( SpiderHttpServer httpServer )
	{
		this.httpServer = httpServer;
	}
}
